package com.iptv.grape.domain;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.ReadJsonFile;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 收藏夹信息
public class BookmarkList {
    private HashMap<String,Bookmark> bookmarkHashMap = new HashMap<String, Bookmark>();

    public HashMap<String, Bookmark> getBookmarkHashMap() {
        return bookmarkHashMap;
    }

    public void setBookmarkHashMap(HashMap<String, Bookmark> bookmarkHashMap) {
        this.bookmarkHashMap = bookmarkHashMap;
    }

    public boolean isEmpty(){
        return this.bookmarkHashMap == null;
    }

    public void init(){
        this.bookmarkHashMap = new HashMap<String, Bookmark>();
    }


    public String toJson(){
//        if(this.bookmarkHashMap == null){
//            this.bookmarkHashMap = new HashMap<String, Bookmark>();
//        }
        Gson gson = new Gson();
        String json = gson.toJson(this.toList());

        //Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
        //Type type = new TypeToken<HashMap<String, Bookmark>>() {}.getType();
        //String json = gson.toJson(this.bookmarkHashMap,type);
        AppLog.getCurLog().out(json);
        return json;
    }

    // 序列化到文件
    public void serializeToFile(){
        String json = this.toJson();
        ReadJsonFile.writeFile("bookmark.json",json);
        AppLog.getCurLog().out("序列化收藏历史到文件");
    }

    // 反序列化对象
    public static BookmarkList unSerializeToFile(){

        BookmarkList bookmarkList = new BookmarkList();
        try {
            if(ReadJsonFile.fileExists("bookmark.json")) {
                String json = ReadJsonFile.readFile("bookmark.json");
                Type type = new TypeToken<List<Bookmark>>() {
                }.getType();

                List<Bookmark> bookmarks = new Gson().fromJson(json, type);
                bookmarkList.bookmarkHashMap = new HashMap<String, Bookmark>();
                for (int i = 0; i < bookmarks.size(); i++) {
                    Bookmark bookmark = bookmarks.get(i);
                    if (!bookmarkList.bookmarkHashMap.containsKey(bookmark.getResourcesID())) {
                        bookmarkList.bookmarkHashMap.put(bookmark.getResourcesID(), bookmark);
                    }
                }
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
           // AppLog.getCurLog().out("");
        }
        return bookmarkList;
    }

    // 移除已经收藏的对象
    public void del(Bookmark bookmark){
        if(bookmarkHashMap.containsKey(bookmark.getResourcesID())) {
            bookmarkHashMap.remove(bookmark.getResourcesID());
        }
        this.serializeToFile();
    }
    // 添加一个对象
    public void add(Bookmark bookmark){
//        if(bookmarkHashMap.containsKey(bookmark.getResourcesID())) {
//            bookmarkHashMap.remove(bookmark.getResourcesID());
//        }
        bookmarkHashMap.put(bookmark.getResourcesID(), bookmark);
        this.serializeToFile();
    }
    // 是否已经收藏
    public boolean isResourcesInBookmark(Bookmark bookmark) {
        return bookmarkHashMap.containsKey(bookmark.getResourcesID());
    }

    // 返回已经收藏的记录
    public List<Bookmark> toList() {
        List<Bookmark> list = new ArrayList<Bookmark>();
        for(Map.Entry<String,Bookmark> item:this.bookmarkHashMap.entrySet()){
            list.add(item.getValue());
        }
        return list;
    }
}
